package auth

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	v1 "gitee.com/liuxuezhan/ar-platform/rpc-client/authRoleClient/api/role/v1"

	"github.com/zeromicro/go-zero/core/logx"
)

type CheckRolesPermissionLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 检测角色功能是否有权限
func NewCheckRolesPermissionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckRolesPermissionLogic {
	return &CheckRolesPermissionLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CheckRolesPermissionLogic) CheckRolesPermission(req *types.CheckRolesPermissionRequest) (resp *types.CheckRolesPermissionResponse, err error) {
	reply, err := l.svcCtx.AuthRoleRpc.CheckRolesPermission(l.ctx, &v1.CheckRolesPermissionRequest{
		RoleIds:              req.RoleIds,
		FunctionId:           req.FunctionId,
		PermissionIdentifier: req.PermissionIdentifier,
	})
	if err != nil {
		return resp, err
	}

	return &types.CheckRolesPermissionResponse{HasPermission: reply.GetHasPermission()}, nil
}
